package com.framework.modules.sys.controller;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.framework.common.utils.QuickHelper;
import com.framework.config.DatabaseInitConfig;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.framework.common.annotation.DataLogAnnotation;
import com.framework.common.utils.PageUtils;
import com.framework.common.utils.R;
import com.framework.common.validator.ValidatorUtils;
import com.framework.modules.sys.entity.SysConfigEntity;
import com.framework.modules.sys.service.SysConfigService;

/**
 * 系统配置信息
 */
@RestController
@RequestMapping("/sys/config")
@Api(tags = "配置管理")
public class SysConfigController extends AbstractController {
	@Autowired
	private SysConfigService sysConfigService;

	@RequestMapping("/get")
	public R get_config_item(@RequestParam("name") String name) {
		SysConfigEntity entity = sysConfigService.selectOne(new EntityWrapper<SysConfigEntity>().eq("param_key", name));
		if (entity == null) {
			return R.error(String.format("对不起，没有找到配置项 %s", name));
		}

		String type = entity.getType();
		R response = R.ok().put("entity", entity);
		if (type.equals("object")) {
			String str = entity.getParamValue();
			Object json = JSON.parseObject(str, Feature.OrderedField);
			response.put("data", json);
		}
		return response;
	}

	/**
	 * 所有配置列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:config:list")
	@ApiOperation("所有配置")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = sysConfigService.queryPage(params);
		return R.ok().put("page", page);
	}

	/**
	 * 配置信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:config:info")
	@ApiOperation("配置信息")
	public R info(@PathVariable("id") Long id) {
		SysConfigEntity config = sysConfigService.selectById(id);

		return R.ok().put("config", config);
	}

	/**
	 * 保存配置
	 */
	@DataLogAnnotation("保存配置")
	@RequestMapping("/save")
	@RequiresPermissions("sys:config:save")
	@ApiOperation("保存配置")
	public R save(@RequestBody SysConfigEntity config) {
		ValidatorUtils.validateEntity(config);
		sysConfigService.save(config);
		return R.ok();
	}

	/**
	 * 修改配置
	 */
	@DataLogAnnotation("修改配置")
	@RequestMapping("/update")
	@RequiresPermissions("sys:config:update")
	@ApiOperation("更新配置")
	public R update(@RequestBody SysConfigEntity config) {
		ValidatorUtils.validateEntity(config);
		sysConfigService.update(config);
		return R.ok();
	}

	/**
	 * 删除配置
	 */
	@DataLogAnnotation("删除配置")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:config:delete")
	@ApiOperation("删除配置")
	public R delete(@RequestBody Long[] ids) {
		sysConfigService.deleteBatch(ids);
		return R.ok();
	}

}